/**
 * 解构赋值
 */
console.log('解构赋值');

// 数组解构赋值
let [a, b, c] = [1, 2, 3];
console.log(a, b, c);

// 对象解构赋值
let { name, age } = { name: 'JG', age: 20 };
console.log(name, age);

// 字符串解构赋值
let [a1, b1, c1, d1, e1] = 'hello';
console.log(a1, b1, c1, d1, e1);


const arr =[11,33,22];
const [a11,b11,c11]= arr;
console.log(a11, b11, c11);

//默认值
const [a111 = 1] = []; // a 是 1
const { b111 = 2 } = { b: undefined }; // b 是 2
const { c111 = 2 } = { c: null }; // c 是 null
console.log(a111,b111,c111);

//数组解构
const foo = ["one", "two", "three"];

const [red, yellow, green] = foo;
console.log(red); // "one"
console.log(yellow); // "two"
console.log(green); // "three"

// 对象解构
const o = { p: 42, q: true };
const { p, q } = o;
console.log(o);

//解构比源更多的元素
const foo1 = ["one", "two"];

const [red1, yellow1, green1, blue] = foo1;
console.log(red1); // "one"
console.log(yellow1); // "two"
console.log(green1); // undefined
console.log(blue); //undefined

//交换变量
let xx = 12,yy =13;
[xx,yy] = [yy,xx];
console.log(xx,yy);

const arr1=[1,2,3];

[arr1[2],arr1[1]] = [arr1[1],arr1[2]];
console.log(arr1);

//解析从函数返回的数组

function ff() {
  return [1, 2, 3];
}
const [a2,,c2]=ff();
console.log(a2,c2);


//使用绑定模式作为剩余属性
const [a3, b3, ...{ pop, push }] = [1, 2];
console.log(a3, b3); // 1 2
console.log(pop, push); // [Function pop] [Function push]


const [a4, b4, ...[c4, d]] = [1, 2, 3, 4];
console.log(a4, b4, c4, d); // 1 2 3 4

//连环嵌套
const [a5, b5, ...[c5, d5, ...[e, f]]] = [1, 2, 3, 4, 5, 6];
console.log(a5, b5, c5, d5, e, f); // 1 2 3 4 5 6